import React, {useEffect, createRef, useState, useMemo} from 'react';
import BasicInfo from "@/views/policyManagement/car/BasicInfo.tsx";
import InsureInfo from "@/views/policyManagement/car/InsureInfo.tsx";
import InsuredInfo from "@/views/policyManagement/car/InsuredInfo.tsx";
import VehicleInfo from "@/views/policyManagement/car/VehicleInfo.tsx";
import AgentInfo from "@/views/policyManagement/car/AgentInfo.tsx";
import TypesInsurance from "@/views/policyManagement/car/TypesInsurance.tsx";
import FileInfo from "@/views/policyManagement/car/FileInfo.tsx";
import {Button} from "antd";
import {queryForUpdateCar} from "@/axios/apis";
import {useSearchParams} from "react-router-dom";
import PropTypes from 'prop-types';

const CarInsuranceDetails = (props) => {
    const {formData, oaSubmit} = props
    const BasicRef = createRef()
    const InsureRef = createRef()
    const InsuredRef = createRef()
    const VehicleRef = createRef()
    const AgentRef = createRef()
    const RiskRef = createRef()
    const FileRef = createRef()
    const [projectCode, setProjectCode] = useState(undefined)
    const [searchParams] = useSearchParams();
    const [policyDisabled, setPolicyDisabled] = useState<boolean>(false)
    const [form, setForm] = useState({
        carBasicInfo: {},
        carInsureInfo: {},
        carInsuredInfo: []
    })
    const [insureInfo, setInsureInfo] = useState({});
    useEffect(() => {
        // 保单明细
        if (searchParams.get("orderId")) {
            queryForUpdateCar({
                orderId: searchParams.get("orderId")
            }).then(res => {
                if (+res.code === 200) {
                    setData(res.data)
                }
            })
        }
        setPolicyDisabled(searchParams.get("type") === 'details')
    }, []);
    useEffect(() => {
        if(formData&&Object.keys(formData).length){
            setData(formData)
        }
    }, [formData]);
    const setData = (data:object) => {
        data.carInsureInfo.countryCode = data.carInsureInfo.countryCode || '008zg'
        setProjectCode(data.carBasicInfo.projectCode)
        setForm(data)
    }
    const save = async () => {
        const params = {}
        if (BasicRef.current) {
            params.carBasicInfo = await BasicRef.current.verifyTheForm();
        }
        if (InsureRef.current) {
            params.carInsureInfo = await InsureRef.current.verifyTheForm();
        }
        if (InsuredRef.current) {
            const flag = InsuredRef.current.verifyTheForm();
            if (!flag) return
            params.carInsuredInfo = flag
        }
        if (VehicleRef.current) {
            params.carAutomobileInfo = await VehicleRef.current.verifyTheForm();
        }
        if (AgentRef.current) {
            // params.carAutomobileInfo = await AgentInfo.current.verifyTheForm();
            const flag = AgentRef.current.verifyTheForm();
            if (!flag) return
            params.carAgentInfo = flag
        }
        if (RiskRef.current) {
            params.carRiskInfo = await RiskRef.current.verifyTheForm();
        }
        if (FileRef.current) {
            params.carFileInfo = FileRef.current.verifyTheForm();
        }
        if(params?.carBasicInfo?.projectType){
            params.projectType = params.carBasicInfo.projectType;
        }
        console.log('p', params)
        oaSubmit(params)
    }
    return (
        <div>
            <BasicInfo ref={BasicRef} carBasicInfo={form.carBasicInfo} setProjectCode={setProjectCode}
                       policyDisabled={policyDisabled} />
            <InsureInfo ref={InsureRef} carInsureInfo={form.carInsureInfo} setValue={setInsureInfo}
                        policyDisabled={policyDisabled} />
            <InsuredInfo ref={InsuredRef} carInsuredInfo={form.carInsuredInfo} insure={insureInfo}
                         policyDisabled={policyDisabled} />
            <VehicleInfo ref={VehicleRef} carAutomobileInfo={form.carAutomobileInfo} policyDisabled={policyDisabled} />
            <AgentInfo ref={AgentRef} carAgentInfo={form.carAgentInfo} projectCode={projectCode}
                       policyDisabled={policyDisabled} />
            <TypesInsurance ref={RiskRef} carRiskInfo={form.carRiskInfo} policyDisabled={policyDisabled} />
            <FileInfo ref={FileRef} carFileInfo={form.carFileInfo} policyDisabled={policyDisabled} />
            <div style={{height: 40, lineHeight: 6, textAlign: 'center'}}>
                {!policyDisabled?<Button type="primary" onClick={save}> 保存 </Button>:null}
            </div>
        </div>
    );
};
CarInsuranceDetails.PropTypes ={
    formData: PropTypes.object,
    oaSubmit: PropTypes.func
}
export default CarInsuranceDetails;